<template>
  <div>
    <!-- Header 区域 -->
    <van-nav-bar fixed title="文章详情" left-arrow @click-left="$router.back()" />
    <!-- 文章等待加载中 -->
    <!-- 一个对象判断为空：
    1、随便在对象里取个属性为undefined
    2、Object.keys(objName).length===0  Object.keys(objName)原地留下的是一个数组
    -->
    <van-loading color="#1989fa" v-if="Object.keys(DetailObj).length === 0">文章加载中...</van-loading>
    <!-- 把内容和评论用大盒子包起来，和文章加载做判断，不包起来没法给两部分加v-elsev -->
    <div v-else>
      <!-- 文章信息区域 -->
      <div class="article-container">
        <!-- 文章标题 -->
        <h1 class="art-title">{{ DetailObj.title }}</h1>

        <!-- 用户信息 -->
        <van-cell center title=" DetailObj.aut_name" :label="formatDate(DetailObj.pubdate)">
          <template #icon>
            <img :src="DetailObj.aut_photo" alt="" class="avatar">
          </template>
          <template #default>
            <div>
              <van-button type="info" size="mini" v-if="DetailObj.is_followed" @click="followFn(true)">已关注</van-button>
              <van-button icon="plus" type="info" size="mini" plain v-else @click="followFn(false)">关注</van-button>
            </div>
          </template>
        </van-cell>

        <!-- 分割线 -->
        <van-divider></van-divider>

        <!-- 文章内容 -->
        <div class="art-content" v-html="DetailObj.content"></div>

        <!-- 分割线 -->
        <van-divider>End</van-divider>

        <!-- 点赞(文章)
      attitude:用户对文章的态度, -1: 无态度，0-不喜欢，1-点赞
       返回字段给了变量就直接用，不要定义
       -->
        <div class="like-box">
          <van-button icon="good-job" type="danger" size="small" v-if="DetailObj.attitude === 1"
            @click="loveFn(true)">已点赞</van-button>
          <van-button icon="good-job-o" type="danger" plain size="small" v-else @click="loveFn(false)">点赞</van-button>
        </div>
      </div>
      <!-- 文章评论 -->
      <div>
        <CommentList></CommentList>
      </div>
    </div>

  </div>
</template>

<script>
import { getArticleDetailAPI, followUserAPI, noFollowUserAPI, loveArticleAPI, noLoveArticleAPI } from '@/api'
import { timeAgo } from '@/utils/date.js'
import CommentList from './CommentList.vue'
export default {
  name: 'ArticleDetail',
  data() {
    return {
      DetailObj: {} // 文章详情对象
    }
  },
  async created() {
    const res = await getArticleDetailAPI({
      // 路由传参-问号传参用query-路径传参(:)用params--路由路径传参用route---跳转路由用router
      artId: this.$route.query.art_id
    })
    console.log(res)
    this.DetailObj = res.data.data
  },
  methods: {
    formatDate: timeAgo,
    // 关注-取关(作者)
    async followFn(bool) {
      if (bool === true) {
        // 用户点在了已关注按钮
        // 页面->显示关注按钮
        this.DetailObj.is_followed = false
        // 业务->取关
        // 调接口->取关接口
        const res = await noFollowUserAPI({
          userId: this.DetailObj.aut_id
        })
        console.log(res)
      } else {
        // 用户点在了关注按钮
        //  // 页面->显示已关注按钮
        this.DetailObj.is_followed = true
        // 业务->关注
        // 调接口->关注接口
        const res = await followUserAPI({
          userId: this.DetailObj.aut_id
        })
        console.log(res)
      }
    },
    // 点赞-取消点赞(文章)
    async loveFn(bool) {
      if (bool === true) {
        // 用户点在了已点赞按钮
        // 页面->显示点赞按钮
        this.DetailObj.attitude = 0
        // 业务->取消点赞
        // 调接口->取消点赞接口
        const res = await noLoveArticleAPI({
          artId: this.DetailObj.art_id
        })
        console.log(res)
      } else {
        // 用户点在了取消点赞按钮
        //  // 页面->显示点赞按钮
        this.DetailObj.attitude = 1
        // 业务->点赞
        // 调接口->点赞接口
        const res = await loveArticleAPI({
          artId: this.DetailObj.art_id
        })
        console.log(res)
      }
    }
  },
  components: {
    CommentList
  }

}
</script>

<style scoped lang="less">
.article-container {
  padding: 10px;
  margin-top: 46px;
}

.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;

  /deep/ img {
    width: 100%;
  }

  /deep/ pre {
    white-space: pre-wrap;
    word-wrap: break-word;
  }
}

.van-cell {
  padding: 5px 0;

  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}

//加载中的居中样式
.van-loading {
  text-align: center;
  padding-top: 46px;
}
</style>
